﻿namespace Tomers.WPF.Localization
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Globalization;
    using System.Text;
    using System.Threading;
    using System.Windows;

    /// <summary>
    /// Represents language context. Sealed class.
    /// </summary>
    public sealed class LanguageContext : INotifyPropertyChanged
    {
        #region Fields
        /// <summary>
        /// Language context.
        /// </summary>
        public static readonly LanguageContext Instance = new LanguageContext();

        private CultureInfo cultureInfo;
        private LanguageDictionary dictionary;

        #endregion

        #region Initialization
        private LanguageContext()
        {
        }
        #endregion

        /// <summary>
        /// PropertyChanged event.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #region Properties
        /// <summary>
        /// Gets or sets the culture.
        /// </summary>
        public CultureInfo Culture
        {
            get
            {
                if (this.cultureInfo == null)
                {
                    this.cultureInfo = CultureInfo.CurrentUICulture;
                }

                return this.cultureInfo;
            }

            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("Culture must not be null");
                }

                if (value == this.cultureInfo)
                {
                    return;
                }

                if (this.cultureInfo != null)
                {
                    LanguageDictionary currentDictionary = LanguageDictionary.GetDictionary(this.cultureInfo);
                    currentDictionary.Unload();
                }

                this.cultureInfo = value;
                LanguageDictionary newDictionary = LanguageDictionary.GetDictionary(this.cultureInfo);
                Thread.CurrentThread.CurrentUICulture = this.cultureInfo;
                newDictionary.Load();
                this.Dictionary = newDictionary;
                this.OnPropertyChanged("Culture");
            }
        }

        /// <summary>
        /// Gets or sets the dictionary.
        /// </summary>
        public LanguageDictionary Dictionary
        {
            get
            {
                return this.dictionary;
            }

            set
            {
                if (value != null && value != this.dictionary)
                {
                    this.dictionary = value;
                    this.OnPropertyChanged("Dictionary");
                }
            }
        }

        #endregion

        #region INotifyPropertyChanged Members
        private void OnPropertyChanged(string property)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
